package com.one.web.manage;

import com.one.entity.User;
import com.one.service.UserService;
import com.one.web.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

/**
 * 管理员登录交互类
 * developer 扶立伟
 */
@WebServlet("/manage/login")
public class ManagerLogin extends BaseServlet{
    private static void login() {
        Scanner sc = new Scanner(System.in);
        System.out.println("---管理员登录---");
        System.out.print("请输入用户名：");
        String username = sc.next();
        System.out.print("请输入密码：");
        String password = sc.next();
        User user = new UserService().login(username, password);
        if(user!=null){
            if (user.getUser_type()==1)
                System.out.println("登录成功");
            else
                System.out.println("登录失败");
        }else{
            System.out.println("登录失败");
        }
    }


    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String m = getM(req);
       switch (m){
           case "index"://登录页面
               //toLogin();
               dispatcher_manage("login", req, resp);
               break;
           case "doLogin"://登录验证
               doLogin(req,resp);
               break;
           case "doExit"://退出登录
               //doExit();
               break;

       }
    }

    private void doLogin(HttpServletRequest req, HttpServletResponse resp) {
        //1.获取账户和密码
        String admin = req.getParameter("admin");
        String password = req.getParameter("password");
        //2.获取管理员对象
        User manager = new UserService().login(admin, password);
        //3.判空和判断是否是管理员
        if(manager==null //为空
            ||manager.getUser_type()==0){//为普通用户，返回登录界面，提示用户不存在或者密码错误
            req.setAttribute("result", "false");
            req.setAttribute("msg", "用户不存在或者密码错误");

        }

        //4.登录成功，转发到管理系统内部




    }


}
